package operation.modules.operation.service.impl;

import freemarker.template.TemplateException;
import operation.android.util.AndroidAutoUpdate;
import operation.android.util.AndroidAutoWrite;
import operation.android.util.OutFilePath;
import operation.android.util.StringUtil;
import operation.modules.operation.service.AndroidService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import static operation.android.util.StringUtil.BigHumpToLine;
import static operation.android.util.StringUtil.Hump;

@Service("androidService")
public class AndroidServiceImpl implements AndroidService {
    private final Logger logger = LoggerFactory.getLogger(getClass());


    /**
     * 生成安卓代码实现方法
     * @param params
     * @return
     */
    public String makeCode(Map<String, Object> params){
        StringUtil stringUtil = new StringUtil();
        // 路径
        String path = (String) params.get("path");
        OutFilePath outFilePath = new OutFilePath();
        outFilePath.setAndroidOutPath(path);
        // 主页面项目名称处理
        String ClassName = (String) params.get("ClassName");
        params.put("class_name", BigHumpToLine(ClassName));
        params.put("className", Hump(ClassName));
        params.put("classname", ClassName.toLowerCase());
        // 左页面
        String leftClassName = (String) params.get("leftClassName");
        if(leftClassName != ""){
            params.put("left-class_name", BigHumpToLine(leftClassName));
            params.put("left-className", Hump(leftClassName));
            params.put("left-classname", leftClassName.toLowerCase());
        }
        // 右页面
        String rightClassName = (String) params.get("rightClassName");
        if(rightClassName != ""){
            params.put("right-class_name", BigHumpToLine(rightClassName));
            params.put("left-className", Hump(rightClassName));
            params.put("left-classname", leftClassName.toLowerCase());
        }





        return writeAndroidAction(params);
    }

    /**
     * 输出方法
     * @param map
     */
    public String writeAndroidAction(Map<String, Object> map){
        String res = "";
        // 引入模板生成类
        AndroidAutoWrite androidAutoWrite = new AndroidAutoWrite();
        // 引入文件修改类
        AndroidAutoUpdate androidAutoUpdate = new AndroidAutoUpdate();
        // 引入项目路径类
        OutFilePath outFilePath = new OutFilePath();
        // 先创建存放界面文件夹
        File file=new File(outFilePath.getAndroidProjectPath() + outFilePath.getUiPath() + map.get("classname"));
        if(!file.exists()){//如果文件夹不存在
            file.mkdir();//创建文件夹
            System.out.println("新建文件夹成功!");
        }
        // 生成Activity代码
        String resActivity = null;
        try {
            resActivity = androidAutoWrite.ActivityAutoWrite(map);
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (TemplateException ex) {
            ex.printStackTrace();
        }
        res = res + resActivity + "\n";
        // 生成ViewModel代码
        String resViewModel = null;
        try {
            resViewModel = androidAutoWrite.ViewModelAutoWrite(map);
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (TemplateException ex) {
            ex.printStackTrace();
        }
        res = res + resViewModel + "\n";
        // 生成activity_verification_xxx.xml 代码
        String resInfoXML = null;
        try {
            resInfoXML = androidAutoWrite.InfoXMLAutoWrite(map);
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (TemplateException ex) {
            ex.printStackTrace();
        }
        res = res + resInfoXML + "\n";

        // 判断是否生成Card
        if(map.get("isCardView").equals(true)){
            // 生成ItemViewModel
            String resItemViewModel = null;
            try {
                resItemViewModel = androidAutoWrite.ItemViewModelAutoWrite(map);
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (TemplateException ex) {
                ex.printStackTrace();
            }
            res = res + resItemViewModel + "\n";
            // 生成Adapter
            String resAdapter = null;
            try {
                resAdapter = androidAutoWrite.AdapterAutoWrite(map);
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (TemplateException ex) {
                ex.printStackTrace();
            }
            res = res + resAdapter + "\n";
            // 生成DetailXML
            String resDetailXML = null;
            try {
                resDetailXML = androidAutoWrite.DetailXMLAutoWrite(map);
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (TemplateException ex) {
                ex.printStackTrace();
            }
            res = res + resDetailXML + "\n";
        }
        // 修改 AndroidViewModelFactory.java
        String resAndroidViewModelFactory = androidAutoUpdate.AndroidViewModelFactory(map);
        res = res + resAndroidViewModelFactory + "\n";
        // 修改 VerificationViewModel.java
        String resVerificationViewModel = androidAutoUpdate.VerificationViewModel(map);
        res = res + resVerificationViewModel + "\n";
        // 修改 AndroidManifest.xml
        String resAndroidManifestl = androidAutoUpdate.AndroidManifest(map);
        res = res + resAndroidManifestl + "\n";
        // 修改 EntityConstant.java
        String resEntityConstant = androidAutoUpdate.EntityConstant(map);
        res = res + resEntityConstant + "\n";

        return res;
    }

}
